
/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
Date Generated       : 2016/3/21 1:39:13
===============================================================================
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;



namespace yncTech.BusinessObjects
{
	/// <summary>
	/// Encapsulates the 'T_S_Users' table
	/// </summary>

    [DebuggerDisplay("Data = {Debug}")]
	[Serializable]
	[DataContract]
	[KnownType(typeof(TSUsers))]	
	[XmlType("TSUsers")]
	public partial class TSUsers : esTSUsers
	{	
		[DebuggerBrowsable(DebuggerBrowsableState.RootHidden | DebuggerBrowsableState.Never)]
		protected override esEntityDebuggerView[] Debug
		{
			get { return base.Debug; }
		}

		override public esEntity CreateInstance()
		{
			return new TSUsers();
		}
		
		#region Static Quick Access Methods
		static public void Delete(System.Decimal userID)
		{
			var obj = new TSUsers();
			obj.UserID = userID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save();
		}

	    static public void Delete(System.Decimal userID, esSqlAccessType sqlAccessType)
		{
			var obj = new TSUsers();
			obj.UserID = userID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save(sqlAccessType);
		}
		#endregion

		
					
		
	
	}



    [DebuggerDisplay("Count = {Count}")]
	[Serializable]
	[CollectionDataContract]
	[XmlType("TSUsersCollection")]
	public partial class TSUsersCollection : esTSUsersCollection, IEnumerable<TSUsers>
	{
		public TSUsers FindByPrimaryKey(System.Decimal userID)
		{
			return this.SingleOrDefault(e => e.UserID == userID);
		}

		
		
		#region WCF Service Class
		
		[DataContract]
		[KnownType(typeof(TSUsers))]
		public class TSUsersCollectionWCFPacket : esCollectionWCFPacket<TSUsersCollection>
		{
			public static implicit operator TSUsersCollection(TSUsersCollectionWCFPacket packet)
			{
				return packet.Collection;
			}

			public static implicit operator TSUsersCollectionWCFPacket(TSUsersCollection collection)
			{
				return new TSUsersCollectionWCFPacket() { Collection = collection };
			}
		}
		
		#endregion
		
				
	}



    [DebuggerDisplay("Query = {Parse()}")]
	[Serializable]	
	public partial class TSUsersQuery : esTSUsersQuery
	{
		public TSUsersQuery():this("TSUsersQuery")
		{
		}
		public TSUsersQuery(string joinAlias)
		{
			this.es.JoinAlias = joinAlias;
		}	

		override protected string GetQueryName()
		{
			return "TSUsersQuery";
		}
		
					
	
		#region Explicit Casts
		
		public static explicit operator string(TSUsersQuery query)
		{
			return TSUsersQuery.SerializeHelper.ToXml(query);
		}

		public static explicit operator TSUsersQuery(string query)
		{
			return (TSUsersQuery)TSUsersQuery.SerializeHelper.FromXml(query, typeof(TSUsersQuery));
		}
		
		#endregion		
	}

	[DataContract]
	[Serializable]
	abstract public partial class esTSUsers : esEntity
	{
		public esTSUsers()
		{

		}
		
		#region LoadByPrimaryKey
		public virtual bool LoadByPrimaryKey(System.Decimal userID)
		{
			if(this.es.Connection.SqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(userID);
			else
				return LoadByPrimaryKeyStoredProcedure(userID);
		}

		public virtual bool LoadByPrimaryKey(esSqlAccessType sqlAccessType, System.Decimal userID)
		{
			if (sqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(userID);
			else
				return LoadByPrimaryKeyStoredProcedure(userID);
		}

		private bool LoadByPrimaryKeyDynamic(System.Decimal userID)
		{
			TSUsersQuery query = new TSUsersQuery();
			query.Where(query.UserID == userID);
			return this.Load(query);
		}

		private bool LoadByPrimaryKeyStoredProcedure(System.Decimal userID)
		{
			esParameters parms = new esParameters();
			parms.Add("UserID", userID);
			return this.Load(esQueryType.StoredProcedure, this.es.spLoadByPrimaryKey, parms);
		}
		#endregion
		
		#region Properties
		
		
		
		/// <summary>
		/// Maps to T_S_Users.UserID
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Decimal? UserID
		{
			get
			{
				return base.GetSystemDecimal(TSUsersMetadata.ColumnNames.UserID);
			}
			
			set
			{
				if(base.SetSystemDecimal(TSUsersMetadata.ColumnNames.UserID, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.UserID);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.LoginName
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String LoginName
		{
			get
			{
				return base.GetSystemString(TSUsersMetadata.ColumnNames.LoginName);
			}
			
			set
			{
				if(base.SetSystemString(TSUsersMetadata.ColumnNames.LoginName, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.LoginName);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.Password
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String Password
		{
			get
			{
				return base.GetSystemString(TSUsersMetadata.ColumnNames.Password);
			}
			
			set
			{
				if(base.SetSystemString(TSUsersMetadata.ColumnNames.Password, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.Password);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.UserType
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String UserType
		{
			get
			{
				return base.GetSystemString(TSUsersMetadata.ColumnNames.UserType);
			}
			
			set
			{
				if(base.SetSystemString(TSUsersMetadata.ColumnNames.UserType, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.UserType);
				}
			}
		}		
		
		/// <summary>
		/// 1:Active    0:InActive
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int16? IsActive
		{
			get
			{
				return base.GetSystemInt16(TSUsersMetadata.ColumnNames.IsActive);
			}
			
			set
			{
				if(base.SetSystemInt16(TSUsersMetadata.ColumnNames.IsActive, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.IsActive);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.LastLoginDate
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? LastLoginDate
		{
			get
			{
				return base.GetSystemDateTime(TSUsersMetadata.ColumnNames.LastLoginDate);
			}
			
			set
			{
				if(base.SetSystemDateTime(TSUsersMetadata.ColumnNames.LastLoginDate, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.LastLoginDate);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.CreatedBy
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String CreatedBy
		{
			get
			{
				return base.GetSystemString(TSUsersMetadata.ColumnNames.CreatedBy);
			}
			
			set
			{
				if(base.SetSystemString(TSUsersMetadata.ColumnNames.CreatedBy, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.CreatedBy);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.CreatedTime
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? CreatedTime
		{
			get
			{
				return base.GetSystemDateTime(TSUsersMetadata.ColumnNames.CreatedTime);
			}
			
			set
			{
				if(base.SetSystemDateTime(TSUsersMetadata.ColumnNames.CreatedTime, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.CreatedTime);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.LastUpdatedBy
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String LastUpdatedBy
		{
			get
			{
				return base.GetSystemString(TSUsersMetadata.ColumnNames.LastUpdatedBy);
			}
			
			set
			{
				if(base.SetSystemString(TSUsersMetadata.ColumnNames.LastUpdatedBy, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.LastUpdatedBy);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Users.LastUpdatedTime
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? LastUpdatedTime
		{
			get
			{
				return base.GetSystemDateTime(TSUsersMetadata.ColumnNames.LastUpdatedTime);
			}
			
			set
			{
				if(base.SetSystemDateTime(TSUsersMetadata.ColumnNames.LastUpdatedTime, value))
				{
					OnPropertyChanged(TSUsersMetadata.PropertyNames.LastUpdatedTime);
				}
			}
		}		
		
		#endregion	

		#region .str() Properties
		
		public override void SetProperties(IDictionary values)
		{
			foreach (string propertyName in values.Keys)
			{
				this.SetProperty(propertyName, values[propertyName]);
			}
		}
		
		public override void SetProperty(string name, object value)
		{
			esColumnMetadata col = this.Meta.Columns.FindByPropertyName(name);
			if (col != null)
			{
				if(value == null || value is System.String)
				{				
					// Use the strongly typed property
					switch (name)
					{							
						case "UserID": this.str().UserID = (string)value; break;							
						case "LoginName": this.str().LoginName = (string)value; break;							
						case "Password": this.str().Password = (string)value; break;							
						case "UserType": this.str().UserType = (string)value; break;							
						case "IsActive": this.str().IsActive = (string)value; break;							
						case "LastLoginDate": this.str().LastLoginDate = (string)value; break;							
						case "CreatedBy": this.str().CreatedBy = (string)value; break;							
						case "CreatedTime": this.str().CreatedTime = (string)value; break;							
						case "LastUpdatedBy": this.str().LastUpdatedBy = (string)value; break;							
						case "LastUpdatedTime": this.str().LastUpdatedTime = (string)value; break;
					}
				}
				else
				{
					switch (name)
					{	
						case "UserID":
						
							if (value == null || value is System.Decimal)
								this.UserID = (System.Decimal?)value;
								OnPropertyChanged(TSUsersMetadata.PropertyNames.UserID);
							break;
						
						case "IsActive":
						
							if (value == null || value is System.Int16)
								this.IsActive = (System.Int16?)value;
								OnPropertyChanged(TSUsersMetadata.PropertyNames.IsActive);
							break;
						
						case "LastLoginDate":
						
							if (value == null || value is System.DateTime)
								this.LastLoginDate = (System.DateTime?)value;
								OnPropertyChanged(TSUsersMetadata.PropertyNames.LastLoginDate);
							break;
						
						case "CreatedTime":
						
							if (value == null || value is System.DateTime)
								this.CreatedTime = (System.DateTime?)value;
								OnPropertyChanged(TSUsersMetadata.PropertyNames.CreatedTime);
							break;
						
						case "LastUpdatedTime":
						
							if (value == null || value is System.DateTime)
								this.LastUpdatedTime = (System.DateTime?)value;
								OnPropertyChanged(TSUsersMetadata.PropertyNames.LastUpdatedTime);
							break;
					

						default:
							break;
					}
				}
			}
            else if (this.ContainsColumn(name))
            {
                this.SetColumn(name, value);
            }
			else
			{
				throw new Exception("SetProperty Error: '" + name + "' not found");
			}
		}		

		public esStrings str()
		{
			if (esstrings == null)
			{
				esstrings = new esStrings(this);
			}
			return esstrings;
		}

		sealed public class esStrings
		{
			public esStrings(esTSUsers entity)
			{
				this.entity = entity;
			}
			
	
			public System.String UserID
			{
				get
				{
					System.Decimal? data = entity.UserID;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.UserID = null;
					else entity.UserID = Convert.ToDecimal(value);
				}
			}
				
			public System.String LoginName
			{
				get
				{
					System.String data = entity.LoginName;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.LoginName = null;
					else entity.LoginName = Convert.ToString(value);
				}
			}
				
			public System.String Password
			{
				get
				{
					System.String data = entity.Password;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.Password = null;
					else entity.Password = Convert.ToString(value);
				}
			}
				
			public System.String UserType
			{
				get
				{
					System.String data = entity.UserType;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.UserType = null;
					else entity.UserType = Convert.ToString(value);
				}
			}
				
			public System.String IsActive
			{
				get
				{
					System.Int16? data = entity.IsActive;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.IsActive = null;
					else entity.IsActive = Convert.ToInt16(value);
				}
			}
				
			public System.String LastLoginDate
			{
				get
				{
					System.DateTime? data = entity.LastLoginDate;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.LastLoginDate = null;
					else entity.LastLoginDate = Convert.ToDateTime(value);
				}
			}
				
			public System.String CreatedBy
			{
				get
				{
					System.String data = entity.CreatedBy;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedBy = null;
					else entity.CreatedBy = Convert.ToString(value);
				}
			}
				
			public System.String CreatedTime
			{
				get
				{
					System.DateTime? data = entity.CreatedTime;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedTime = null;
					else entity.CreatedTime = Convert.ToDateTime(value);
				}
			}
				
			public System.String LastUpdatedBy
			{
				get
				{
					System.String data = entity.LastUpdatedBy;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.LastUpdatedBy = null;
					else entity.LastUpdatedBy = Convert.ToString(value);
				}
			}
				
			public System.String LastUpdatedTime
			{
				get
				{
					System.DateTime? data = entity.LastUpdatedTime;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.LastUpdatedTime = null;
					else entity.LastUpdatedTime = Convert.ToDateTime(value);
				}
			}
			

			private esTSUsers entity;
		}
		
		[NonSerialized]
		private esStrings esstrings;		
		
		#endregion
		
		#region Housekeeping methods

		override protected IMetadata Meta
		{
			get
			{
				return TSUsersMetadata.Meta();
			}
		}

		#endregion		
		
		#region Query Logic

		public TSUsersQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSUsersQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSUsersQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return this.Query.Load();
		}
		
		protected void InitQuery(TSUsersQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntity)this).Connection;
			}			
		}

		#endregion
		
        [IgnoreDataMember]
		private TSUsersQuery query;		
	}



	[Serializable]
	abstract public partial class esTSUsersCollection : esEntityCollection<TSUsers>
	{
		#region Housekeeping methods
		override protected IMetadata Meta
		{
			get
			{
				return TSUsersMetadata.Meta();
			}
		}

		protected override string GetCollectionName()
		{
			return "TSUsersCollection";
		}

		#endregion		
		
		#region Query Logic

	#if (!WindowsCE)
		[BrowsableAttribute(false)]
	#endif
		public TSUsersQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSUsersQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSUsersQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return Query.Load();
		}

		override protected esDynamicQuery GetDynamicQuery()
		{
			if (this.query == null)
			{
				this.query = new TSUsersQuery();
				this.InitQuery(query);
			}
			return this.query;
		}

		protected void InitQuery(TSUsersQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntityCollection)this).Connection;
			}			
		}

		protected override void HookupQuery(esDynamicQuery query)
		{
			this.InitQuery((TSUsersQuery)query);
		}

		#endregion
		
		private TSUsersQuery query;
	}



	[Serializable]
	abstract public partial class esTSUsersQuery : esDynamicQuery
	{
		override protected IMetadata Meta
		{
			get
			{
				return TSUsersMetadata.Meta();
			}
		}	
		
		#region QueryItemFromName
		
        protected override esQueryItem QueryItemFromName(string name)
        {
            switch (name)
            {
				case "UserID": return this.UserID;
				case "LoginName": return this.LoginName;
				case "Password": return this.Password;
				case "UserType": return this.UserType;
				case "IsActive": return this.IsActive;
				case "LastLoginDate": return this.LastLoginDate;
				case "CreatedBy": return this.CreatedBy;
				case "CreatedTime": return this.CreatedTime;
				case "LastUpdatedBy": return this.LastUpdatedBy;
				case "LastUpdatedTime": return this.LastUpdatedTime;

                default: return null;
            }
        }		
		
		#endregion
		
		#region esQueryItems

		public esQueryItem UserID
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.UserID, esSystemType.Decimal); }
		} 
		
		public esQueryItem LoginName
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.LoginName, esSystemType.String); }
		} 
		
		public esQueryItem Password
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.Password, esSystemType.String); }
		} 
		
		public esQueryItem UserType
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.UserType, esSystemType.String); }
		} 
		
		public esQueryItem IsActive
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.IsActive, esSystemType.Int16); }
		} 
		
		public esQueryItem LastLoginDate
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.LastLoginDate, esSystemType.DateTime); }
		} 
		
		public esQueryItem CreatedBy
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.CreatedBy, esSystemType.String); }
		} 
		
		public esQueryItem CreatedTime
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.CreatedTime, esSystemType.DateTime); }
		} 
		
		public esQueryItem LastUpdatedBy
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.LastUpdatedBy, esSystemType.String); }
		} 
		
		public esQueryItem LastUpdatedTime
		{
			get { return new esQueryItem(this, TSUsersMetadata.ColumnNames.LastUpdatedTime, esSystemType.DateTime); }
		} 
		
		#endregion
		
	}


	
	public partial class TSUsers : esTSUsers
	{

				
		#region TSUserInfo - One To One
		/// <summary>
		/// One to One
		/// Foreign Key Name - T_S_UserInfo_T_S_Users_FK1
		/// </summary>

		[XmlIgnore]
		public TSUserInfo TSUserInfo
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._TSUserInfo == null)
				{
					this._TSUserInfo = new TSUserInfo();
					this._TSUserInfo.es.Connection.Name = this.es.Connection.Name;
					this.SetPostOneSave("TSUserInfo", this._TSUserInfo);
				
					if(this.UserID != null)
					{
						this._TSUserInfo.Query.Where(this._TSUserInfo.Query.UserID == this.UserID);
						this._TSUserInfo.Query.Load();
					}
				}

				return this._TSUserInfo;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._TSUserInfo != null) 
				{ 
					this.RemovePostOneSave("TSUserInfo"); 
					this._TSUserInfo = null;
					
				} 
			}          			
		}
		
		
		private TSUserInfo _TSUserInfo;
		#endregion

		#region UpToTSRoleCollection - Many To Many
		/// <summary>
		/// Many to Many
		/// Foreign Key Name - T_S_UserRole_FK1
		/// </summary>

		[XmlIgnore]
		public TSRoleCollection UpToTSRoleCollection
		{
			get
			{
				if(this._UpToTSRoleCollection == null)
				{
					this._UpToTSRoleCollection = new TSRoleCollection();
					this._UpToTSRoleCollection.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("UpToTSRoleCollection", this._UpToTSRoleCollection);
					if (!this.es.IsLazyLoadDisabled && this.UserID != null)
					{
						TSRoleQuery m = new TSRoleQuery("m");
						TSUserRolesQuery j = new TSUserRolesQuery("j");
						m.Select(m);
						m.InnerJoin(j).On(m.RoleID == j.RoleID);
                        m.Where(j.UserID == this.UserID);

						this._UpToTSRoleCollection.Load(m);
					}
				}

				return this._UpToTSRoleCollection;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._UpToTSRoleCollection != null) 
				{ 
					this.RemovePostSave("UpToTSRoleCollection"); 
					this._UpToTSRoleCollection = null;
					
				} 
			}  			
		}

		/// <summary>
		/// Many to Many Associate
		/// Foreign Key Name - T_S_UserRole_FK1
		/// </summary>
		public void AssociateTSRoleCollection(TSRole entity)
		{
			if (this._TSUserRolesCollection == null)
			{
				this._TSUserRolesCollection = new TSUserRolesCollection();
				this._TSUserRolesCollection.es.Connection.Name = this.es.Connection.Name;
				this.SetPostSave("TSUserRolesCollection", this._TSUserRolesCollection);
			}

			TSUserRoles obj = this._TSUserRolesCollection.AddNew();
			obj.UserID = this.UserID;
			obj.RoleID = entity.RoleID;
		}

		/// <summary>
		/// Many to Many Dissociate
		/// Foreign Key Name - T_S_UserRole_FK1
		/// </summary>
		public void DissociateTSRoleCollection(TSRole entity)
		{
			if (this._TSUserRolesCollection == null)
			{
				this._TSUserRolesCollection = new TSUserRolesCollection();
				this._TSUserRolesCollection.es.Connection.Name = this.es.Connection.Name;
				this.SetPostSave("TSUserRolesCollection", this._TSUserRolesCollection);
			}

			TSUserRoles obj = this._TSUserRolesCollection.AddNew();
			obj.UserID = this.UserID;
            obj.RoleID = entity.RoleID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
		}

		private TSRoleCollection _UpToTSRoleCollection;
		private TSUserRolesCollection _TSUserRolesCollection;
		#endregion

		#region TSUserRolesCollectionByUserID - Zero To Many
		
		static public esPrefetchMap Prefetch_TSUserRolesCollectionByUserID
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = yncTech.BusinessObjects.TSUsers.TSUserRolesCollectionByUserID_Delegate;
				map.PropertyName = "TSUserRolesCollectionByUserID";
				map.MyColumnName = "UserID";
				map.ParentColumnName = "UserID";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void TSUserRolesCollectionByUserID_Delegate(esPrefetchParameters data)
		{
			TSUsersQuery parent = new TSUsersQuery(data.NextAlias());

			TSUserRolesQuery me = data.You != null ? data.You as TSUserRolesQuery : new TSUserRolesQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.UserID == me.UserID);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - T_S_UserRole_FK1
		/// </summary>

		[XmlIgnore]
		public TSUserRolesCollection TSUserRolesCollectionByUserID
		{
			get
			{
				if(this._TSUserRolesCollectionByUserID == null)
				{
					this._TSUserRolesCollectionByUserID = new TSUserRolesCollection();
					this._TSUserRolesCollectionByUserID.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("TSUserRolesCollectionByUserID", this._TSUserRolesCollectionByUserID);
				
					if (this.UserID != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._TSUserRolesCollectionByUserID.Query.Where(this._TSUserRolesCollectionByUserID.Query.UserID == this.UserID);
							this._TSUserRolesCollectionByUserID.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._TSUserRolesCollectionByUserID.fks.Add(TSUserRolesMetadata.ColumnNames.UserID, this.UserID);
					}
				}

				return this._TSUserRolesCollectionByUserID;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._TSUserRolesCollectionByUserID != null) 
				{ 
					this.RemovePostSave("TSUserRolesCollectionByUserID"); 
					this._TSUserRolesCollectionByUserID = null;
					
				} 
			} 			
		}
			
		
		private TSUserRolesCollection _TSUserRolesCollectionByUserID;
		#endregion

		
		protected override esEntityCollectionBase CreateCollectionForPrefetch(string name)
		{
			esEntityCollectionBase coll = null;

			switch (name)
			{
				case "TSUserRolesCollectionByUserID":
					coll = this.TSUserRolesCollectionByUserID;
					break;	
			}

			return coll;
		}		
		/// <summary>
		/// Used internally by the entity's hierarchical properties.
		/// </summary>
		protected override List<esPropertyDescriptor> GetHierarchicalProperties()
		{
			List<esPropertyDescriptor> props = new List<esPropertyDescriptor>();
			
			props.Add(new esPropertyDescriptor(this, "TSUserRolesCollectionByUserID", typeof(TSUserRolesCollection), new TSUserRoles()));
		
			return props;
		}
		
		/// <summary>
		/// Called by ApplyPostSaveKeys 
		/// </summary>
		/// <param name="coll">The collection to enumerate over</param>
		/// <param name="key">"The column name</param>
		/// <param name="value">The column value</param>
		private void Apply(esEntityCollectionBase coll, string key, object value)
		{
			foreach (esEntity obj in coll)
			{
				if (obj.es.IsAdded)
				{
					obj.SetProperty(key, value);
				}
			}
		}
		
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PostSave.
		/// </summary>
		protected override void ApplyPostSaveKeys()
		{
			if(this._TSUserRolesCollection != null)
			{
				Apply(this._TSUserRolesCollection, "UserID", this.UserID);
			}
			if(this._TSUserRolesCollectionByUserID != null)
			{
				Apply(this._TSUserRolesCollectionByUserID, "UserID", this.UserID);
			}
		}
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PostOneToOneSave.
		/// </summary>
		protected override void ApplyPostOneSaveKeys()
		{
			if(this._TSUserInfo != null)
			{
				if(this._TSUserInfo.es.IsAdded)
				{
					this._TSUserInfo.UserID = this.UserID;
				}
			}
		}
		
	}
	



	[Serializable]
	public partial class TSUsersMetadata : esMetadata, IMetadata
	{
		#region Protected Constructor
		protected TSUsersMetadata()
		{
			m_columns = new esColumnMetadataCollection();
			esColumnMetadata c;

			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.UserID, 0, typeof(System.Decimal), esSystemType.Decimal);
			c.PropertyName = TSUsersMetadata.PropertyNames.UserID;
			c.IsInPrimaryKey = true;
			c.IsAutoIncrement = true;
			c.NumericPrecision = 18;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.LoginName, 1, typeof(System.String), esSystemType.String);
			c.PropertyName = TSUsersMetadata.PropertyNames.LoginName;
			c.CharacterMaxLength = 20;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.Password, 2, typeof(System.String), esSystemType.String);
			c.PropertyName = TSUsersMetadata.PropertyNames.Password;
			c.CharacterMaxLength = 20;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.UserType, 3, typeof(System.String), esSystemType.String);
			c.PropertyName = TSUsersMetadata.PropertyNames.UserType;
			c.CharacterMaxLength = 3;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.IsActive, 4, typeof(System.Int16), esSystemType.Int16);
			c.PropertyName = TSUsersMetadata.PropertyNames.IsActive;
			c.NumericPrecision = 5;
			c.HasDefault = true;
			c.Default = @"((1))";
			c.Description = "1:Active    0:InActive";
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.LastLoginDate, 5, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = TSUsersMetadata.PropertyNames.LastLoginDate;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.CreatedBy, 6, typeof(System.String), esSystemType.String);
			c.PropertyName = TSUsersMetadata.PropertyNames.CreatedBy;
			c.CharacterMaxLength = 40;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.CreatedTime, 7, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = TSUsersMetadata.PropertyNames.CreatedTime;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.LastUpdatedBy, 8, typeof(System.String), esSystemType.String);
			c.PropertyName = TSUsersMetadata.PropertyNames.LastUpdatedBy;
			c.CharacterMaxLength = 40;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSUsersMetadata.ColumnNames.LastUpdatedTime, 9, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = TSUsersMetadata.PropertyNames.LastUpdatedTime;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
		}
		#endregion	
	
		static public TSUsersMetadata Meta()
		{
			return meta;
		}	
		
		public Guid DataID
		{
			get { return base.m_dataID; }
		}	
		
		public bool MultiProviderMode
		{
			get { return false; }
		}		

		public esColumnMetadataCollection Columns
		{
			get	{ return base.m_columns; }
		}
		
		#region ColumnNames
		public class ColumnNames
		{ 
			 public const string UserID = "UserID";
			 public const string LoginName = "LoginName";
			 public const string Password = "Password";
			 public const string UserType = "UserType";
			 public const string IsActive = "IsActive";
			 public const string LastLoginDate = "LastLoginDate";
			 public const string CreatedBy = "CreatedBy";
			 public const string CreatedTime = "CreatedTime";
			 public const string LastUpdatedBy = "LastUpdatedBy";
			 public const string LastUpdatedTime = "LastUpdatedTime";
		}
		#endregion	
		
		#region PropertyNames
		public class PropertyNames
		{ 
			 public const string UserID = "UserID";
			 public const string LoginName = "LoginName";
			 public const string Password = "Password";
			 public const string UserType = "UserType";
			 public const string IsActive = "IsActive";
			 public const string LastLoginDate = "LastLoginDate";
			 public const string CreatedBy = "CreatedBy";
			 public const string CreatedTime = "CreatedTime";
			 public const string LastUpdatedBy = "LastUpdatedBy";
			 public const string LastUpdatedTime = "LastUpdatedTime";
		}
		#endregion	

		public esProviderSpecificMetadata GetProviderMetadata(string mapName)
		{
			MapToMeta mapMethod = mapDelegates[mapName];

			if (mapMethod != null)
				return mapMethod(mapName);
			else
				return null;
		}
		
		#region MAP esDefault
		
		static private int RegisterDelegateesDefault()
		{
			// This is only executed once per the life of the application
			lock (typeof(TSUsersMetadata))
			{
				if(TSUsersMetadata.mapDelegates == null)
				{
					TSUsersMetadata.mapDelegates = new Dictionary<string,MapToMeta>();
				}
				
				if (TSUsersMetadata.meta == null)
				{
					TSUsersMetadata.meta = new TSUsersMetadata();
				}
				
				MapToMeta mapMethod = new MapToMeta(meta.esDefault);
				mapDelegates.Add("esDefault", mapMethod);
				mapMethod("esDefault");
			}
			return 0;
		}			

		private esProviderSpecificMetadata esDefault(string mapName)
		{
			if(!m_providerMetadataMaps.ContainsKey(mapName))
			{
				esProviderSpecificMetadata meta = new esProviderSpecificMetadata();			


				meta.AddTypeMap("UserID", new esTypeMap("numeric", "System.Decimal"));
				meta.AddTypeMap("LoginName", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("Password", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("UserType", new esTypeMap("char", "System.String"));
				meta.AddTypeMap("IsActive", new esTypeMap("smallint", "System.Int16"));
				meta.AddTypeMap("LastLoginDate", new esTypeMap("datetime", "System.DateTime"));
				meta.AddTypeMap("CreatedBy", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("CreatedTime", new esTypeMap("datetime", "System.DateTime"));
				meta.AddTypeMap("LastUpdatedBy", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("LastUpdatedTime", new esTypeMap("datetime", "System.DateTime"));			
				
				
				
				meta.Source = "T_S_Users";
				meta.Destination = "T_S_Users";
				
				meta.spInsert = "proc_T_S_UsersInsert";				
				meta.spUpdate = "proc_T_S_UsersUpdate";		
				meta.spDelete = "proc_T_S_UsersDelete";
				meta.spLoadAll = "proc_T_S_UsersLoadAll";
				meta.spLoadByPrimaryKey = "proc_T_S_UsersLoadByPrimaryKey";
				
				this.m_providerMetadataMaps["esDefault"] = meta;
			}
			
			return this.m_providerMetadataMaps["esDefault"];
		}

		#endregion

		static private TSUsersMetadata meta;
		static protected Dictionary<string, MapToMeta> mapDelegates;
		static private int _esDefault = RegisterDelegateesDefault();
	}
}
